package com.global.college.ui.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;

import com.global.college.R;
import com.global.college.UserData;
import com.global.college.base.BaseActivity;
import com.global.college.ui.interfaces.IModifyPswView;
import com.global.college.ui.presenter.ModifyPswPresenter;
import com.global.college.utils.NetworkUtils;
import com.global.college.utils.StringUtils;
import com.global.college.utils.ToastUtils;
import com.global.college.views.ClearEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 修改密码
 */
public class ModifyPswActivity extends BaseActivity implements IModifyPswView {


    @BindView(R.id.et_psw_origin)
    ClearEditText etPswOrigin;
    @BindView(R.id.et_psw_new)
    ClearEditText etPswNew;
    @BindView(R.id.et_psw_new_again)
    ClearEditText etPswNewAgain;
    @BindView(R.id.tv_save)
    TextView tvSave;

    private String pswOrigin, pswNew, pswNewAgain;
    private ModifyPswPresenter modifyPswPresenter;

    @Override
    public int getContentViewId() {
        return R.layout.activity_modify_psw;
    }

    @Override
    public void onCreateNew(Bundle savedInstanceState) {
        setShowTitleBar(true);
        setShowBack(true);
        setTitle("修改密码");
        modifyPswPresenter = new ModifyPswPresenter(this);
        modifyPswPresenter.setModifyPswView(this);
    }

    @Override
    public void getInitData() {
        initListener();
        tvSave.setEnabled(false);
    }

    private void initListener() {
        etPswOrigin.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                pswOrigin = charSequence.toString();
                pswNew = etPswNew.getText().toString();
                pswNewAgain = etPswNewAgain.getText().toString();
                if (StringUtils.isNotEmpty(pswOrigin, true) && StringUtils.isNotEmpty(pswNew, true) && StringUtils.isNotEmpty(pswNewAgain, true)) {
                    tvSave.setEnabled(true);
                    tvSave.setBackgroundResource(R.drawable.selector_button_red);
                    tvSave.setTextColor(getResources().getColor(R.color.white));
                } else {
                    tvSave.setEnabled(false);
                    tvSave.setBackgroundResource(R.drawable.selector_button_grey);
                    tvSave.setTextColor(getResources().getColor(R.color.white));
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        etPswNew.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                pswOrigin = etPswOrigin.getText().toString();
                pswNew = charSequence.toString();
                pswNewAgain = etPswNewAgain.getText().toString();
                if (StringUtils.isNotEmpty(pswOrigin, true) && StringUtils.isNotEmpty(pswNew, true) && StringUtils.isNotEmpty(pswNewAgain, true)) {
                    tvSave.setEnabled(true);
                    tvSave.setBackgroundResource(R.drawable.selector_button_red);
                    tvSave.setTextColor(getResources().getColor(R.color.white));
                } else {
                    tvSave.setEnabled(false);
                    tvSave.setBackgroundResource(R.drawable.selector_button_grey);
                    tvSave.setTextColor(getResources().getColor(R.color.white));
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        etPswNewAgain.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                pswOrigin = etPswOrigin.getText().toString();
                pswNew = etPswNew.getText().toString();
                pswNewAgain = charSequence.toString();
                if (StringUtils.isNotEmpty(pswOrigin, true) && StringUtils.isNotEmpty(pswNew, true) && StringUtils.isNotEmpty(pswNewAgain, true)) {
                    tvSave.setEnabled(true);
                    tvSave.setBackgroundResource(R.drawable.selector_button_red);
                    tvSave.setTextColor(getResources().getColor(R.color.white));
                } else {
                    tvSave.setEnabled(false);
                    tvSave.setBackgroundResource(R.drawable.selector_button_grey);
                    tvSave.setTextColor(getResources().getColor(R.color.white));
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.tv_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_save:
                modifyPsw();
                break;
        }
    }

    private void modifyPsw() {
        if (!NetworkUtils.isNetworkAvailable(this)) {
            ToastUtils.showCenterToast(R.string.your_device_has_not_netword);
            return;
        } else {
            pswOrigin = etPswOrigin.getText().toString();
            pswNew = etPswNew.getText().toString();
            pswNewAgain = etPswNewAgain.getText().toString();
            if (StringUtils.isEmpty(pswOrigin)) {
                ToastUtils.showCenterToast(
                        R.string.please_input_your_psw_2);
                return;
            }
            if (6 > StringUtils.getLength(pswNew, true)
                    || 16 < StringUtils.getLength(pswNew, true)) {
                ToastUtils.showCenterToast(
                        R.string.please_input_from_six_to_sixteen_psw);
                return;
            }
            if (6 > StringUtils.getLength(pswNewAgain, true)
                    || 16 < StringUtils.getLength(pswNewAgain, true)) {
                ToastUtils.showCenterToast(
                        R.string.please_input_from_six_to_sixteen_psw);
                return;
            }
            if (!StringUtils.equals(pswNew,pswNewAgain)){
                ToastUtils.showCenterToast("两次输入的新密码不一致");
                return;
            }
                //修改密码
                modifyPswPresenter.editPsw(pswNewAgain,pswNew,pswOrigin, UserData.getInstance().getPhone());
        }

    }

    @Override
    public void onFindPswSuccess(Object resetPswModel) {

    }

    @Override
    public void onFindPswFail(String msg) {

    }

    @Override
    public void onGetSmsCodeSuccess(Object smsModel) {

    }

    @Override
    public void onGetSmsCodeFail(String msg) {

    }

    @Override
    public void editPwdSuccess(Object smsModel) {
        ToastUtils.showCenterToast("操作成功！");
        finish();
    }

    @Override
    public void editPwdFail(Object smsModel) {

    }
}